package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import common.Constant;

import dao.UserDao;

import util.MyJson;
import util.ParameterCheck;

/**
 * Servlet implementation class Reg
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Register() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		String email = ParameterCheck.stringDefault(request.getParameter("email"));
		String pwd = ParameterCheck.stringDefault(request.getParameter("pwd")).replace(" ", "");
		String name=ParameterCheck.stringDefault(request.getParameter("name"));
		String vcode=ParameterCheck.stringDefault(request.getParameter("vcode")).toLowerCase();
		
		String scode=ParameterCheck.stringDefault(String.valueOf(request.getSession().getAttribute("ccode"))).toLowerCase();
		if(null==request.getSession().getAttribute("ccode") || !vcode.equals(scode)){
			pw.write(MyJson.toJson("msg","抱歉，您输入的验证码错误，请重试，谢谢！"));
		}		
		else if (null == email || "".equals(email.trim()))
			pw.write(MyJson.toJson("msg","抱歉，注册请输入正确的email 地址，谢谢！"));
		else if (null == pwd || "".equals(pwd.trim()))
			pw.write(MyJson.toJson("msg","抱歉，密码不能为空，谢谢！"));
		else if (email.length() > 100) {
			pw.write(MyJson.toJson("msg","抱歉，您的email:" + email + " 地址太长不能注册，谢谢！"));
		} 
		else if(null == name || "".equals(name.trim())){
			pw.write(MyJson.toJson("msg","抱歉，昵称不能为空，谢谢！"));
		}else {
			UserDao userDao = new UserDao();
			int uid = userDao.reg(email, pwd,name);
			if (-1 > uid) {
				pw.write(MyJson.toJson("msg","抱歉，系统繁忙:" + uid + ",请您稍后再试！"));
			}else if(-1==uid){
				pw.write(MyJson.toJson("msg","抱歉，昵称: " + name + " 已经被注册了，请您换一个吧"));
			}
			else if (0 == uid) {
				pw.write(MyJson.toJson("msg","抱歉，Email： " + email + " 已经被注册了，请您换一个吧"));
			} else if (0 < uid) {
				pw.write(MyJson.toJson("msg", "0"));
				Cookie cookie=new Cookie("user", util.KeyGen.getKey(uid));
				cookie.setMaxAge(Constant.CookieExpired);
				cookie.setPath("/");
				response.addCookie(cookie);	
			}
		}
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
